package com.gai.shop.service.impl;

import com.gai.shop.dao.UserDao;
import com.gai.shop.dao.impl.UserDaoImpl;
import com.gai.shop.entity.User;
import com.gai.shop.service.UserService;
import com.gai.shop.util.MD5Utils;
import com.gai.shop.util.StrUtils;

import java.util.List;

public class UserServiceImpl implements UserService {
    private final UserDao UD = new UserDaoImpl();

    /**
     * 通过用户名查询用户
     * @param uname
     * @return
     */
    @Override
    public User selectUserByUname(String uname) {
        User user = null;
        try {
            //如果是null或者是"" 就返回false
            if (StrUtils.empty(uname)){
                user = UD.selectUserByUname(uname);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @Override
    public Integer addUser(User user) {
        Integer result = 0;
        try {
            if (user != null){
                result = UD.addUser(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 邮件激活业务
     * @param uemail
     * @param ucode
     * @return
     */
    @Override
    public Integer activeUser(String uemail, String ucode) {
        User user = null;
        try {
            //1 判断 uemail ucode 是否为空
            if (StrUtils.empty(uemail,ucode)){
                //2 通过uemail ucode查询是否有对应用户存在
                user = UD.selectUserByUemailAndUcode(uemail,ucode);
                if (user == null){//有这个用户
                    return 0;
                }
                //3 获取查询到的用户的id来进行一个激活处理
                //4 给出响应 激活失败 0，激活成功 1，已经激活 2
                if (user.getUstatus() == 1){
                    return  2;
                }else {
                    //处理激活的事件
                    Integer result = UD.updateUserUstatusByUid(user.getUid());
                    if (result > 0){
                        return 1;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 登录
     * @param uname
     * @param upassword
     * @return
     */
    @Override
    public User login(String uname, String upassword) {
        try {
            if (StrUtils.empty(uname,upassword)){
                return null;
            }
            //进行判断用户名和密码是否正确
            User user = UD.selectUserByUname(uname);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @return
     */
    @Override
    public List<User> userList() {
        List<User> userList = null;
        try {
            userList = UD.userList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userList;
    }

    /**
     * 动态查询
     * @param uname
     * @param usex
     * @return
     */
    @Override
    public List<User> userList(String uname, String usex) {
        List<User> userList = null;
        try {
            userList = UD.userList(uname, usex);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userList;
    }

    /**
     * 删除用户
     * @param uid
     * @return
     */
    @Override
    public Integer deleteUser(Integer uid) {
        Integer result = 0;
        try {
            if (uid > 0){
                result = UD.deleteUser(uid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
